###### Maintext: Figure 3
###### Effect of Visibility on Subject Utility
gc(); rm(list = ls()); set.seed(12345)
setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) # Note: if you are not using R Studio this command will not work, set WD to source file location manually

############### SCRIPT SUMMARY #####################

packages <- c("dplyr", "Rmisc", "ggplot2")
lapply(packages, require, character.only = T)
source("functions.R")
dfExp <- read.csv("data/cleaned/uganda_jun18_cleaned.csv", 
                  stringsAsFactors = F)
dfExp <- dfExp[!is.na(dfExp$rand_vm), ]

#############################################################################################
################################## SURVEY EXPERIMENT FIGURE #################################
#############################################################################################

dfExp$vm_ladder_change <- dfExp$vm_ladder - 10 # Pos = higher on ladder, Neg = lower on ladder
depVar <- "vm_ladder"
treatVar <- "rand_vm"
dfList <- list(dfExp, 
               dfExp[dfExp$vm_purchase == 1, ], 
               dfExp[dfExp$vm_purchase == 2, ])
namesList <- c("Full Sample", "Purchased Soap", "Purchased Airtime")
dfMeans <- c()
for(i in 1:length(dfList)){
  dfUsing <- dfList[[i]]
  means <-  Rmisc::summarySE(dfUsing[!is.na(dfUsing[, treatVar]), ], 
                             measurevar = depVar, 
                             groupvars = treatVar, na.rm = T)
  means$dataset <- namesList[i]
  dfMeans <- rbind(dfMeans, means)
}
alpha <- 0.05
dfMeans$conf.high <- dfMeans[, depVar] + dfMeans$ci
dfMeans$conf.low <- dfMeans[, depVar] - dfMeans$ci
dfMeans$term <- dfMeans$rand_vm
dfMeans$estimate <- dfMeans$vm_ladder

dfPlot <- dfMeans
dfPlot$term <- gsub(pattern = treatVar, 
                    replacement = "", 
                    dfPlot$term)
dfPlot$Sample <- factor(dfPlot$dataset, 
                        levels = c("Purchased Airtime", 
                                   "Purchased Soap", "Full Sample"))
p <- ggplot(dfPlot, aes(x = term, y = estimate, colour = Sample)) + 
  geom_linerange(aes(x = term, ymin = conf.low,
                     ymax = conf.high, size = I(10)),
                 lwd = 1.1, position = position_dodge(width = 1/2)) +
  geom_pointrange(aes(x = term, y = estimate, ymin =  conf.low,
                      ymax = conf.high),
                  lwd = 0.4, position = position_dodge(width = 1/2),
                  shape = 21, fill = "WHITE", fatten = 6) + 
  coord_flip() +  xlab("") +   theme_gray() +  
  ylab("Ladder Position (10 = Anchor)") +
  ylim(6, 14) +
  geom_hline(yintercept = 10, color = "red", linetype = 2, lwd = 0.5)  + 
  ggtitle("Effects of Treatments on Ladder Position")
p <- p + scale_color_grey(end = 0, start = 0.8)
p <- p + theme_custom(legend_position = "bottom", 
                 legend_justification = c(0.5, 0.5), 
                 text_size = 10)

pdf(file = "figures/fig_3.pdf", 
    width = 8, height = 6)
print(p)
dev.off()

save.image(file = "results/fig_3.RData")

